package com.csair.seam.domain.service.impl;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csair.seam.domain.mapper.FaMatchMapper;
import com.csair.seam.domain.model.entity.Equip;
import com.csair.seam.domain.model.entity.FaAsset;
import com.csair.seam.domain.model.entity.FaMatch;
import com.csair.seam.domain.model.entity.FaMatchDetail;
import com.csair.seam.domain.model.entity.FaMatchRatio;
import com.csair.seam.domain.model.param.FaAssetBO;
import com.csair.seam.domain.model.param.FaMatchParam;
import com.csair.seam.domain.service.IFaMatchService;
import com.csair.seam.infrastructure.config.DictionaryConfigurationProperties;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class FaMatchServiceImpl extends ServiceImpl<FaMatchMapper, FaMatch> implements IFaMatchService {
	
	@Autowired
    private RestTemplate restTemplate;
	
	@Autowired
	private DictionaryConfigurationProperties dictionaryConfigurationProperties;

	@Override
	public void addFaMatch() {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.add("Authorization", dictionaryConfigurationProperties.getFaToken());
		SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
	    factory.setReadTimeout(30*60*1000);//单位为ms
	    restTemplate.setRequestFactory(factory);
	    
	    List<Equip> list = getBaseMapper().findFaEquipList();
	    getBaseMapper().deleteAllData();
	    for (Equip equip : list) {
	    	FaAssetBO bo = new FaAssetBO();
	    	bo.setSourceNumber(equip.getCode());
			String json = JSON.toJSONString(bo);
			HttpEntity<String> formEntity = new HttpEntity<String>(json, headers);
			List<FaAsset> resultList;
			try {
				String result = restTemplate.postForObject(dictionaryConfigurationProperties.getFaUrl(), formEntity, String.class);
				resultList = JSONObject.parseArray(result, FaAsset.class);
			} catch (Exception e) {
				log.error(e.getMessage(),e);
				continue;
			}
			if(CollectionUtils.isNotEmpty(resultList)) {
				for (FaAsset faAsset : resultList) {
					FaMatch faMatch = new FaMatch();
					faMatch.setEquipCode(equip.getCode());
					faMatch.setLoc1(faAsset.getLoc1());
					faMatch.setLoc2(faAsset.getLoc2());
					faMatch.setBookTypeCode(faAsset.getBookTypeCode());
					faMatch.setCostCenterCode(faAsset.getCostCenter());
					faMatch.setUsedStatus("在用");
					
					try {
						boolean orgMatch = StringUtils.isNotBlank(equip.getOrgName()) &&  equip.getOrgName().equals(faAsset.getBookTypeCode());
						boolean deptMatch = StringUtils.isNotBlank(equip.getUseDept()) && equip.getUseDept().equals(faAsset.getCostCenter());
						boolean statusMatch = StringUtils.isNotBlank(faMatch.getUsedStatus()) && faMatch.getUsedStatus().equals(equip.getUsedStatusName());

						faMatch.setOrgMatch(orgMatch?"Y":"N");
						faMatch.setDeptMatch(deptMatch?"Y":"N");
						faMatch.setStatusMatch(statusMatch?"Y":"N");
						
						if(orgMatch && deptMatch && statusMatch) {
							faMatch.setMatchFlag("Y");
						}else {
							faMatch.setMatchFlag("N");
						}
						
						getBaseMapper().insert(faMatch);
					} catch (Exception e) {
						log.error(e.getMessage(),e);
					}
				}
			}
		}
	}

	@Override
	public FaMatchRatio findPieChartData(String match) {
		return getBaseMapper().findPieChartData(match);
	}

	@Override
	public List<FaMatchRatio> findHistogramByOrg() {
		return getBaseMapper().findHistogramByOrg();
	}

	@Override
	public List<FaMatchRatio> findHistogramByDept(String orgId) {
		return getBaseMapper().findHistogramByDept(orgId);
	}

	@Override
	public List<FaMatchRatio> findHistogramByStatus() {
		return getBaseMapper().findHistogramByStatus();
	}

	@Override
	public Page<FaMatchDetail> findFaMatchDetailPage(FaMatchParam param) {
		Page<FaMatchDetail> page = new Page<FaMatchDetail>(param.getPageIndex(), param.getPageSize());
		return getBaseMapper().findFaMatchDetail(page, param);
	}

	@Override
	public List<FaMatchDetail> findFaMatchDetailList(FaMatchParam param) {
		return getBaseMapper().findFaMatchDetail(param);
	}

}
